package com.base.jdk8.stream;

import com.base.jdk8.dto.Person;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class HelloStream {


    private List<Person> initData() {
        List<Person> people = new ArrayList<>();
        Person tom1 = new Person(1,"Tom", 6000, "male", "New York");
        Person tom2 = new Person(2,"Tom2", 7000, "male", "New York" );
        Person jack = new Person(3,"jack", 8000, "male", "New York" );
        Person jack2 = new Person(3,"jack3", 8000, "male", "New York" );

        people.add(tom1);
        people.add(tom2);
        people.add(jack);
        people.add(jack2);
        return people;
    }

    // 筛选
    public void filter() {
        List<Person> people = initData();
        people.stream().filter(person -> person.getSalary() > 8000).map(Person::getName).forEach(System.out::println);
    }

    @Test
    public void streamToMap() {
        List<Person> people = initData();
        Map<Integer, String> personMap = people.stream().collect(Collectors.toMap(Person::getPersonId, Person::getName));
        personMap.forEach((key,value) -> {
            System.out.println(key + "---" + value);
        });
        System.out.println(personMap);
    }

}
